
export default class Progress {
    min = 8 * 6
    max = 16 * 6
    cell = 10
    r = null
    $slideTime = null
    constructor(r, start, end, $slideTime){
        this.min = start * 6
        this.max = end * 6
        this.r = r
        this.$slideTime = $slideTime
        this._showSE(start, end)
    }
    _showSE(start, end){
        $('.time_at.start').html(`${start}:00`)
        $('.time_at.end').html(`${end}:00`)
    }

    changeSE(start, end){
        this.min = start * 6
        this.max = end * 6
        this.r.attr('min', this.min)
        this.r.attr('max', this.max)
        this._showSE(start, end)
        let cur = this.r.val()
        if(this.r.val() <= this.min) cur = this.min
        else if(this.r.val() >= this.max) cur = this.max
        this.change(cur, true, true)
    }
    init() {
        const { min, max, r } = this
        r.attr('min', min)
        r.attr('max', max)

        r.on('input', () => this._handleChange(r.val(), true));
        

    }
    _handleChange(v, isCall) {
        const {min, max} = this
        let h = Math.floor(v / 6)
        let m = Math.floor(v % 6 * this.cell)
        
        if(isCall && this.onChangeCall != null){

            this.onChangeCall(h , m)
        }
        
        // console.log(v)
        let n = (v - min) / (max - min) * 100
        
        if(this.onChangePur){
            this.onChangePur(n)
        }
        //todo：14 为滑块大小
        let slideLeft = this.r.width() * n / 100 - 24 * n / 100 + $('#date-progress').position().left  + 10 
        if(this.$slideTime != null){
            this.$slideTime.css({left: slideLeft - this.$slideTime.width() / 2})
            this.$slideTime.text(`${Math.floor(v / 6)}:00`)
        }


        this.r.css({
            'background-image': '-webkit-linear-gradient(left ,rgb(254, 227, 116) 0%,rgb(254, 227, 116) ' + n + '%,#ddd ' + n + '%, #ddd 100%)'
        });
    }
    onChangeCall = null
    onChangePur = null //纯净的变化回调
    change(v, isCall = false, stopPlayer = false){
        this.r.val(v)
        this._handleChange(v, isCall)
        if(stopPlayer){
            this.stopPlayerIcon()
        }
    }
    changeDate(date, isCall = false, stopPlayer = false, changSe = null){
        let v = date.getHours() * 6 + Math.floor(date.getMinutes() / 10)
        if(changSe != null && changSe instanceof Array && changSe.length >= 2){
            this._showSE(...changSe)
        }
        this.change(v, isCall, stopPlayer)
        
    }

    stopPlayerIcon(){
        $('.progress-wrap .player img').attr('src', 'https://xiaoji-hangzhou.oss-cn-hangzhou.aliyuncs.com/img/icon_3dmodel_play.jpeg')
    }
    startPlayerIcon(){
        $('.progress-wrap .player img').attr('src', 'https://xiaoji-hangzhou.oss-cn-hangzhou.aliyuncs.com/img/icon_3dmodel_pause.jpeg')
    }

}

